from django.shortcuts import render
from rest_framework.response import Response
from rest_framework.views import APIView
from .models import *
from .ser import *
# Create your views here.

class Newsmanage(APIView):
    def post(self,request):
        ser = Newsser(data=request.data)
        if ser.is_valid():
            ser.save()
            return Response({'code': 200})
        else:
            print(ser.errors)
            return Response({'code':20010})
    def get(self,request):
        se = News.objects.all()
        ser = Newsser(se,many=True)
        return Response({'code':200,'list':ser.data})

from django.core.paginator import Paginator
class Admanage(APIView):
    def post(self,request):
        ser = Adser(data=request.data)
        if ser.is_valid():
            ser.save()
            return Response({'code':200})
        else:
            print(ser.errors)
            return Response({'code':20010})
    def get(self,request):
        sr = Ad.objects.all()
        p = Paginator(sr,2)
        cpage = request.GET.get('p')
        rel  = p.page(cpage)
        ser = Adser(rel,many=True)
        return Response({'code':200,'list':ser.data,'tpage':p.num_pages})
